{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit modStatsGetRecent;

interface

uses Classes,
     ESBDates,
     ScruteX,
     modStatsPlayers,
     SysUtils;


const
   CNT_FILE_STATS_INFOS = 'recents.ini';

   CNT_FILE_STATS_KEY_FILE_GLOBAL = 'global';
   CNT_FILE_STATS_KEY_FILE_FLOTTE = 'flotte';
   CNT_FILE_STATS_KEY_FILE_RECHERCHE = 'recherche';




type
    TScanStatsRecent = class(TObject)
    private
       FScanner : TScruteDossier;
       FUnivers : integer;

       FStats: TCollectionPlayerStats;

       FLastRecentFileTypeAll: string;
       FLastRecentFileTypeFlottes: string;
       FLastRecentFileTypeRecherche: string;

       FLastRencetDateAll: int64;
       FLastRencetDateFlottes: int64;
       FLastRencetDateRecherche: int64;

       procedure OnFileFound(Sender: TObject; Nom: string);
       procedure SetOnFileFound(Value: TScruteEvent);

    public
       property FileStatsAll       : string read FLastRecentFileTypeAll       write FLastRecentFileTypeAll;
       property FileStatsFlottes   : string read FLastRecentFileTypeFlottes   write FLastRecentFileTypeFlottes;
       property FileStatsRecherche : string read FLastRecentFileTypeRecherche write FLastRecentFileTypeRecherche;

       property xOnFileFound : TScruteEvent write SetOnFileFound;


       // Renvoi le fichier le plus recent pour cet univers
       procedure ScanDir(Dossier : string; Univers : integer);

       function GetFromDir(Dossier: string): boolean;
       function SetToDir(Dossier: string): boolean;


       constructor Create;
       destructor Destroy; override;
    end;

implementation

function TScanStatsRecent.SetToDir(Dossier: string): boolean;
var F: TextFile;
    Fichier: string;
begin

    Result := false;


    if FLastRecentFileTypeAll = '' then begin
       exit;
    end;
    if FLastRecentFileTypeFlottes = '' then begin
       exit;
    end;
    if FLastRecentFileTypeRecherche = '' then begin
       exit;
    end;

    Fichier := Dossier + CNT_FILE_STATS_INFOS;

    AssignFile(F, Fichier);

    {$I-}
    ReWrite(F);
    {$I+}

    // IOResult renvoie 0 => cela signifie que la dernière opération d'E/S a réussi
    if NoT(IOResult = 0) then begin
       exit;
    end;



    WriteLn(F, CNT_FILE_STATS_KEY_FILE_GLOBAL + '=' + ExtractFileName(FLastRecentFileTypeAll));
    WriteLn(F, CNT_FILE_STATS_KEY_FILE_FLOTTE + '=' + ExtractFileName(FLastRecentFileTypeFlottes));
    WriteLn(F, CNT_FILE_STATS_KEY_FILE_RECHERCHE + '=' + ExtractFileName(FLastRecentFileTypeRecherche));
    WriteLn(F, '');


    CloseFile(F);
    
    Result := true;

end;


function TScanStatsRecent.GetFromDir(Dossier: string): boolean;
var F: TextFile;
    Fichier: string;
    Ligne, Key: string;
    xPos: integer;
    iNbCond: integer;
begin

    FLastRecentFileTypeAll := '';
    FLastRecentFileTypeFlottes := '';
    FLastRecentFileTypeRecherche := '';

    Result := false;

    Fichier := Dossier + CNT_FILE_STATS_INFOS;

    if FileExists(Fichier) = false then begin
       exit;
    end;

    AssignFile(F, Fichier);

    {$I-}
    ReSet(F);
    {$I+}

    // IOResult renvoie 0 => cela signifie que la dernière opération d'E/S a réussi
    if NoT(IOResult = 0) then begin
       exit;
    end;

    iNbCond := 0;


    While NoT(EoF(F)) do begin
         // Lit une ligne
         ReadLn(F, Ligne);

         Ligne := trim(Ligne);
         if Ligne = '' then Continue;

         xPos := pos('=', Ligne);

         if xPos = 0 then begin
            Continue;
         end;

         Key := copy(Ligne, 1, xPos - 1);
         System.Delete(Ligne, 1, xPos);

         if SameText(Key, CNT_FILE_STATS_KEY_FILE_GLOBAL) then begin
            FLastRecentFileTypeAll := Ligne;

            if length(FLastRecentFileTypeAll) > 3 then begin
               FLastRecentFileTypeAll := Dossier + FLastRecentFileTypeAll;
               Inc(iNbCond);
            end;


         end else if SameText(Key, CNT_FILE_STATS_KEY_FILE_FLOTTE) then begin
            FLastRecentFileTypeFlottes := Ligne;

            if length(FLastRecentFileTypeFlottes) > 3 then begin
               FLastRecentFileTypeFlottes := Dossier + FLastRecentFileTypeFlottes;
               Inc(iNbCond);
            end;


         end else if SameText(Key, CNT_FILE_STATS_KEY_FILE_RECHERCHE) then begin
            FLastRecentFileTypeRecherche := Ligne;

            if length(FLastRecentFileTypeRecherche) > 3 then begin
               FLastRecentFileTypeRecherche := Dossier + FLastRecentFileTypeRecherche;
               Inc(iNbCond);
            end;

         end;

    end;

    CloseFile(F);

    if iNbCond = 3 then begin
       Result := true;
    end;

end;


procedure TScanStatsRecent.SetOnFileFound(Value: TScruteEvent);
begin
    FScanner.OnFichier := Value;
end;

procedure TScanStatsRecent.OnFileFound(Sender: TObject; Nom: string);
begin

    if FStats.LoadFromFile(Nom, True) > 0 then begin

       if FStats.Univers <> FUnivers then exit;


       case FStats.TypeStats of
           STATS_TYPE_ALL: begin
                              if FStats.DateCreation < FLastRencetDateAll then exit;

                              FLastRencetDateAll := FStats.DateCreation;
                              FLastRecentFileTypeAll := Nom;

                           end;

           STATS_TYPE_FLOTTES: begin
                              if FStats.DateCreation < FLastRencetDateFlottes then exit;

                              FLastRencetDateFlottes := FStats.DateCreation;
                              FLastRecentFileTypeFlottes := Nom;

                           end;

           STATS_TYPE_RECHERCHE: begin
                              if FStats.DateCreation < FLastRencetDateRecherche then exit;

                              FLastRencetDateRecherche := FStats.DateCreation;
                              FLastRecentFileTypeRecherche := Nom;
                           end;

       end;

    end;
    
end;

procedure TScanStatsRecent.ScanDir(Dossier : string; Univers : integer);
begin
    FUnivers   := Univers;

    FScanner.Dossier := Dossier;

    FLastRecentFileTypeAll := '';
    FLastRecentFileTypeFlottes := '';
    FLastRecentFileTypeRecherche := '';

    FLastRencetDateAll := 0;
    FLastRencetDateFlottes := 0;
    FLastRencetDateRecherche := 0;


    FScanner.Execute;

end;


constructor TScanStatsRecent.Create;
begin
    inherited Create;

    FUnivers  := 0;

    FStats   := TCollectionPlayerStats.Create;

    FScanner := TScruteDossier.Create(nil);
    FScanner.OnFichier := OnFileFound;
    FScanner.Filtre := '*.txt';
    FScanner.SousDossier := true;
    FScanner.MaxPenetration := 20;



end;

destructor TScanStatsRecent.Destroy;
begin
    FStats.Free;
    FScanner.Free;

    inherited Destroy;
end;






end.
